package com.ggdog.blog.Services.impl;


import com.ggdog.blog.DAO.UserDAO;
import com.ggdog.blog.POJO.Administrator;
import com.ggdog.blog.POJO.Follow;
import com.ggdog.blog.POJO.Result;
import com.ggdog.blog.POJO.User;
import com.ggdog.blog.Services.UserServices;
import com.ggdog.blog.utils.ConstUtil;
import com.ggdog.blog.utils.JwtUtil;
import com.ggdog.blog.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServicesImpl implements UserServices {
    @Autowired
    private UserDAO userDAO;
    @Override
    public User findUserByName(String userName) {
        User user=userDAO.findUserByName(userName);
        return user;
    }

    @Override
    public void register(String userName, String userPsd) {
        //md5加密
        String md5Psd= Md5Util.getMD5String(userPsd);
        userDAO.addUser(userName,md5Psd);
    }

    @Override
    public Administrator findAdminByUserId(int userId) {
        return userDAO.findAdminByUserId(userId);
    }

    @Override
    public User[] getAllUser() {
       return userDAO.getAllUser();
    }

    @Override
    public int updateUserById(int id, String newName,int status) {
       return userDAO.updateUserById(id,newName,status);
    }

    @Override
    public int deleteUserById(int id) {
        return userDAO.deleteUserById(id);
    }

    @Override
    public User getUserById(int id) {
       return userDAO.getUserById(id);
    }

    @Override
    public  Result<String> updateUserAvatarUrlByUID(int id,String url) {
        int result=userDAO.updateUserAvatarUrlByUID(id,url);
        if(result==1){
            User user=getUserById(id);
            String newToken=createUserToken(user);
            return Result.success(newToken);
        }else{
            return Result.error("头像路径更新失败！");
        }
    }

    @Override
    public String createUserToken(User user) {
        //查询是否是管理员
        Administrator administrator=findAdminByUserId(user.getId());
        System.out.println("管理员：");
        System.out.println(administrator);
        Map<String,Object> claims=new HashMap<>();
        claims.put("userId",user.getId());
        claims.put("userName",user.getUserName());
        claims.put("avatarURL",user.getAvatarURL());
        if(administrator!=null){
            claims.put("administratorId",administrator.getAdministratorId());
            claims.put("administratorCategory",administrator.getAdministratorCategory());
        }
        Date date=new Date(System.currentTimeMillis()+1000*60*60);
        String token= JwtUtil.createToken(claims,date);
        return token;
    }

    @Override
    public Result<String> updateUserPsdById(int uid, String oldPassword, String newPassword) {
        User user=getUserById(uid);
        String oldMd5Psd= Md5Util.getMD5String(oldPassword);
        String newMd5Psd=Md5Util.getMD5String(newPassword);
        if(!oldMd5Psd.equals(user.getUserPsd())){
            return Result.error("旧密码错误");
        }
        int result=userDAO.updateUserPsdById(uid,newMd5Psd);
        if(result==1){
            user.setUserPsd(newMd5Psd);
            String token=createUserToken(user);
            return Result.success(token);
        }else{
            return Result.error("密码更新失败！");
        }


    }

    @Override
    public Result<String> updateAdminPermission(int uid, int newPermission) {
        int result=userDAO.updateAdminPermission(uid,newPermission);
        if(result==1){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }

    }

    @Override
    public List<Administrator> getAllAdmin() {
        return userDAO.getAllAdmin();
    }

    @Override
    public Result<String> deleteAdminByAdminId(int adminId) {
        int result=userDAO.deleteAdminByAdminId(adminId);
        if(result==1){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    @Override
    public Result<String> addNewAdminById(int uid,int permission) {
        int result=userDAO.addNewAdminById(uid,permission);
        if(result==1){
            return Result.success("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }
}
